import java.util.Date;


public class Account {
	private Transaction[] transactions;
	private int nbTransactions;
	
	public Account() {
		transactions = new Transaction[2];
	}
	
	public int getBalance() {
		int total = 0;
		for(int i = 0; i < nbTransactions; i++) {
			total += transactions[i].getAmount();
		}
		return total;
	}
	
	private void ensureCapacity() {
		if(nbTransactions == transactions.length) {
			Transaction[] tmp = new Transaction[2 * transactions.length + 1];
			for(int i = 0; i < transactions.length; i++) {
				tmp[i] = transactions[i];
			}
			transactions = tmp;
		}
	}
	
	public void deposit(int amount) {
		if(amount < 0)
			throw new IllegalArgumentException("amount negative");
		ensureCapacity();
		transactions[nbTransactions] = new Transaction(new Date(), amount);
		nbTransactions++;
	}
	
	public void withdraw(int amount) {
		if(amount < 0)
			throw new IllegalArgumentException("amount negative");
		if(getBalance() < amount)
			throw new IllegalArgumentException("cannot overdraw");
		transactions[nbTransactions] = new Transaction(new Date(), -amount);
		nbTransactions++;
	}
	
	public void transferTo(Account other, int amount) {
		if(other == null)
			throw new IllegalArgumentException("target account is null");
		this.withdraw(amount);
		other.deposit(amount);
	}
	
	public void close() {
		if(getBalance() != 0)
			throw new IllegalArgumentException("cannot close account because balance is not equal to zero");
		transactions = null;
		nbTransactions = 0;
	}
}
